package com.book.book.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.book.book.domain.BooklogDO;
import com.book.book.service.BooklogService;
import com.book.common.utils.PageUtils;
import com.book.common.utils.Query;
import com.book.common.utils.R;

/**
 * 漂流记录表
 * 
 * @author siyuan
 * @email 894266364@qq.com
 * @date 2018-03-20 15:56:19
 */
 
@Controller
@RequestMapping("/book/booklog")
public class BooklogController {
	@Autowired
	private BooklogService booklogService;
	
	@GetMapping()
	@RequiresPermissions("book:booklog:booklog")
	String Booklog(){
	    return "book/booklog/booklog";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("book:booklog:booklog")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BooklogDO> booklogList = booklogService.list(query);
		int total = booklogService.count(query);
		PageUtils pageUtils = new PageUtils(booklogList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("book:booklog:add")
	String add(){
	    return "book/booklog/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("book:booklog:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		BooklogDO booklog = booklogService.get(id);
		model.addAttribute("booklog", booklog);
	    return "book/booklog/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("book:booklog:add")
	public R save( BooklogDO booklog){
		if(booklogService.save(booklog)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("book:booklog:edit")
	public R update( BooklogDO booklog){
		booklogService.update(booklog);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("book:booklog:remove")
	public R remove( Integer id){
		if(booklogService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("book:booklog:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		booklogService.batchRemove(ids);
		return R.ok();
	}
	
}
